/*
 * Copyright (c) 2016 . All Rights Reserved.
 */
package com.cgmnx.admin.web.controller;

import com.cgmnx.admin.biz.NoteBiz;
import com.cgmnx.admin.biz.RankNumBiz;
import com.cgmnx.admin.common.bean.ApiRes;
import com.cgmnx.admin.common.util.WebUtils;
import com.cgmnx.admin.dao.condition.NoteCondition;
import com.cgmnx.admin.dao.entity.NoteEntity;
import com.cgmnx.admin.dao.entity.RankNumEntity;
import com.cgmnx.admin.model.enums.NodeAuditStatus;
import com.cgmnx.admin.model.enums.RankNumType;
import com.cgmnx.admin.web.converter.entity.NoteConditionConverter;
import com.cgmnx.admin.web.converter.entity.NoteEntityConverter;
import com.cgmnx.admin.web.converter.vm.NoteVmConverter;
import com.cgmnx.admin.web.model.param.NoteQueryParam;
import com.cgmnx.admin.web.model.param.NoteSaveParam;
import com.cgmnx.admin.web.model.vm.NoteVm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: created  Date: 16/12/12 Time: 下午3:02
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/cgmnx/note")
public class NoteController {

    @Resource
    private NoteBiz noteBiz;

    @Resource
    private RankNumBiz rankNumBiz;

    @Resource
    private NoteVmConverter noteVmConverter;

    @Resource
    private NoteConditionConverter noteConditionConverter;

    @Resource
    private NoteEntityConverter noteEntityConverter;

    @RequestMapping("/queryPage.json")
    public ApiRes queryPage(@RequestBody @Valid NoteQueryParam param, BindingResult result) {

        if (result.hasErrors()) {
            return ApiRes.returnFail(WebUtils.resolveBindingResult(result));
        }
        final NoteCondition condition = noteConditionConverter.convert(param);
        final int count = noteBiz.count(condition);

        if (count < 1) {
            return ApiRes.emptyList();
        }

        List<NoteEntity> entityList = noteBiz.queryPage(condition);

        if (CollectionUtils.isEmpty(entityList)) {
            return ApiRes.emptyList();
        }

        final List<NoteVm> noteVmList = noteVmConverter.convert(entityList);

        return ApiRes.list(noteVmList, count);
    }

    @RequestMapping("/queryStatus.json")
    public ApiRes queryStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            return ApiRes.returnFail("笔记id和状态不能为空");
        }
        final NoteEntity entity = noteBiz.query(id);

        if (Objects.isNull(entity)) {
            return ApiRes.returnFail("笔记不存在");
        }

        noteBiz.audit(id, status, entity.getStatus().getCode());
        if (status == 1) {
            RankNumEntity rankNumEntity = new RankNumEntity();
            rankNumEntity.setRankType(RankNumType.codeOf(0));
            rankNumEntity.setOrders(0);
            rankNumEntity.setRankNum(0);
            rankNumEntity.setNoteId(id);
            rankNumEntity.setCreateUser("张伞");
            rankNumBiz.save(rankNumEntity);
        }
        return ApiRes.returnSuccess("审核成功");
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public ApiRes<NoteVm> query(@RequestParam("id") Integer id) {
        if (Objects.isNull(id)) {
            return ApiRes.returnFail("笔记id和状态不能为空");
        }
        final NoteEntity entity = noteBiz.query(id);
        final NoteVm noteVm = noteVmConverter.convert(entity);
        return new ApiRes<>(noteVm);
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public ApiRes<NoteEntity> save(@RequestBody @Valid NoteSaveParam item, BindingResult result) {

        if (result.hasErrors()) {
            return ApiRes.returnFail(WebUtils.resolveBindingResult(result));
        }

        final NoteEntity convert = noteEntityConverter.convert(item);
        noteBiz.save(convert);

        return ApiRes.returnSuccess();
    }

    @RequestMapping(value = "/queryNodeAuditStatus.json")
    public ApiRes queryNodeAuditStatus() {
        return ApiRes.returnSuccess(getNodeAuditStatusMap());
    }

    private List<Map<String, Object>> getNodeAuditStatusMap() {

        List<Map<String, Object>> list = Lists.newArrayList();
        for (NodeAuditStatus value : NodeAuditStatus.values()) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("code", value.getCode());
            map.put("desc", value.getDesc());
            list.add(map);
        }
        return list;
    }
}